package com.example.ioc.context;

import com.example.common.Statistical;
import com.example.common.utils.Statistics;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

@Slf4j
//@Component 可以用Component注入
public class LInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    private static volatile LInstantiationAwareBeanPostProcessor instance;

    private LInstantiationAwareBeanPostProcessor(){
        Statistics.logInfo(log);
    }

    public static LInstantiationAwareBeanPostProcessor getInstance(){
        if(instance == null){
            synchronized (LInstantiationAwareBeanPostProcessor.class){
                if(instance == null){
                    instance = new LInstantiationAwareBeanPostProcessor();
                }
            }
        }
        return instance;
    }

    @Override
    public Object postProcessBeforeInstantiation(@NonNull Class<?> beanClass, @NonNull String beanName)
            throws BeansException {
        if (beanClass.isAssignableFrom(Statistical.class)) {
            Statistics.logInfo(log, "postProcessBeforeInstantiation", beanName);
        }
        return null;
    }

    @Override
    public PropertyValues postProcessProperties(@NonNull PropertyValues pvs, @NonNull Object bean,
                                                @NonNull String beanName) throws BeansException {
        if (bean instanceof Statistical statistical){
            Statistics.logInfo(log, statistical, "postProcessProperties", bean);
        }
        return pvs;
    }
}
